/*
 * @Author: 我想吃余 483766615@qq.com
 * @Date: 2025-10-19 16:53:44
 * @LastEditors: 我想吃余 483766615@qq.com
 * @LastEditTime: 2025-10-21 09:18:13
 * @FilePath: /code/oscode/fifo/server.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "comm.hpp"
#include "log.hpp"

int main()
{
    Log log;
    log.Enable(Onefile);

    Init init;
    //打开命名管道
    pid_t fd = open(FIFO_FILE, O_RDONLY, 0664);//没有被写打开的话，会阻塞等待
    if(fd < 0) 
    {
        log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(FIFO_OPEN_ERR);
    }
    log.Enable(Classfile);

    log(Info, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    log(Warning, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    log(Fatal, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    log(Debug, "server open file done, error string: %s, error code: %d", strerror(errno), errno);
    
    //开始通信
    while(true)
    {
        char buffer[NUM] = {0};
        int n = read(fd, buffer, sizeof(buffer));
        if(n > 0)
        {
            buffer[n] = 0;
            cout << "client say# " << buffer << endl;
        }
        else if(n == 0)
        {
            log(Debug, "client quit, me too! error string: %s, error code: %d", strerror(errno), errno);
            break;
        }
        else break;
    }
    
    close(fd);
    return 0;
}